###### Descriptive Figures ##### #---------------------------------------------#

# This script generates all descriptive figures in the paper and the Appendix.

# load AAA event data by Dukalskis (2021)
AAAD_events <- read_csv("data/_Authoritarian Actions Abroad Database (online appendix).xlsx - Sheet1.csv")

# load coding periods 
AAAD_codings <- read_delim("data/AAAD_countries_years.csv")

###### Figure 1: Map -------------------------------------------

# Aggregate number of events at the country-year level
AAAD_events_cy <- AAAD_events %>% 
  group_by(country, year) %>% 
  summarise(events = length(ID)) %>% 
  ungroup() %>% 
  mutate(year = as.numeric(year)) %>% 
  drop_na(year)

# Merge event country years with coding periods
AAAD_codings_events <- left_join(AAAD_codings, AAAD_events_cy) %>% 
  mutate(events = ifelse(is.na(events), 0, events))

# choose breaks for map colors
breaks <- c(0,5,10,20,50,100,150,200)

# Aggregate at the country level and bin event frequencies
AAAD_countries <- AAAD_codings_events %>% 
  group_by(country) %>% 
  summarise(events = sum(events)) %>% 
  ungroup() %>% 
  mutate(country = case_when(country == "UAE" ~ "United Arab Emirates",
                             country == "Congo– Brazzaville" ~ "Republic of the Congo",
                             country == "Congo, DRC" ~ "Democratic Republic of the Congo",
                             T ~ country),
         events_log = log(events + 1),
         events_cut_num = case_when(events_log == 0 ~ 0,
                                    events_log >= .5 & events_log <= 2 ~ 5,
                                    events_log >= 2.01 & events_log <= 2.7 ~ 10,
                                    events_log >= 3.0 & events_log <= 3.5 ~ 20,
                                    events_log >= 3.5 & events_log <= 4 ~ 50,
                                    events_log >= 4.1 & events_log <= 4.5 ~ 100,
                                    events_log >= 4.6 & events_log <= 5 ~ 150,
                                    events_log >= 5.01 & events_log <= 6 ~ 200,
                                          T ~ NA_real_ ))

# Map countries
map <- joinCountryData2Map(AAAD_countries,
                           joinCode = "NAME",
                           nameJoinColumn = "country",
                           verbose = T)

# Exclude Antarctica
map <- subset(map, continent != "Antarctica")

# Pick colours (RoW colour scheme, 10-cat)
cols <- c('#ffeda0','#fed976','#feb24c','#fd8d3c','#fc4e2a','#e31a1c','#bd0026','#800026')

#setEPS(height = 7, width = 14, reset = T)
#postscript(file.path("figures/world_map_new.eps"))

pdf(file.path("figures/Figure_1.pdf"))


par(mar=c(1,1,1,0)) #(bottom, left, top, right)

#---------------------------------------------------#
#--------------------Create map-------------------#
#---------------------------------------------------#

# Map
mapParams <- mapCountryData(map, nameColumnToPlot = "events_cut_num",
                            colourPalette = cols,
                            catMethod = breaks, addLegend = F,
                            missingCountryCol = "white",
                            mapTitle = "",borderCol = "black")

# Legend
do.call(addMapLegend, c(mapParams, legendLabels = "all", legendMar = 10,
                        legendWidth = 0.6, legendShrink = 0.775, labelFontSize = 0.8))

#---------------------------------------------------#
# Close plotting device
dev.off()

# save output as .eps
#setEPS(height = 7, width = 14)
#postscript(file.path(OUTDIR, "world_map_libdem.eps"))

# ------------------------------------------- #

###### Figure 2: Time trend -------------------------------------------
AAAD_trend <- AAAD_events  %>% 
  filter(year != "multiple") %>% 
  group_by(year) %>%
  summarise(TR_total = length(ID)) %>% 
  ungroup()

#transnational repression trend since 1990
ggplot(AAAD_trend, aes(x = as.numeric(year), y = TR_total)) +
  geom_line() +
  geom_point() +
  scale_x_continuous(breaks = seq(1990,2020,5)) +
  labs(x = "",
       y = "Transnational repression events") +
  theme_bw()

ggsave("figures/Figure_2.pdf", width = 7, height = 5)

# ------------------------------------------- #


##### Figure 3: Actions  -------------------------------------------
AAAD_actions <- AAAD_events %>% 
  filter(action != "targeted") %>% 
  group_by(action) %>% 
  count() %>% 
  mutate(action = case_when(action == "threatened" ~ "Threatened",
                            action == "abducted" ~ "Abducted",
                            action == "abduction_attempt" ~ "Abduction attempt",
                            action == "arrested/detained" ~ "Arrested/detained",
                            action == "assassinated" ~ "Assassinated",
                            action == "assassination_attempt" ~ "Assassination attempt",
                            action == "attacked" ~ "Attacked",
                            action == "extradited" ~ "Extradited",
                            action == "extradition_attempt" ~ "Extradition attempt",
                            action == "family_threatened" ~ "Family threatened",
                            T ~ NA_character_),
         attempts = ifelse(isTRUE(str_detect(action, "attempt")), "1", "0")) 

# Plot
ggplot(data = AAAD_actions, aes(x = action, y= n, fill = attempts)) +
  geom_bar(position="dodge", stat="identity",
           width = 0.75,  aes(reorder(action, n), n), alpha = .75) +
  theme_bw() +
  scale_fill_grey(start = .3, end = .6) +
  xlab("") +
  ylab("Transnational repression events") +
  coord_flip() +
  theme(legend.position = "none")

ggsave("figures/Figure_3.pdf", width = 7, height = 5,
       dpi = "retina")


##### Figure 4: Targets  -------------------------------------------
AAAD_targets <- AAAD_events %>% 
  group_by(target) %>% 
  count() %>% 
  mutate(target = case_when(target == "citizen" ~ "Citizens",
                            target == "activist" ~ "Activists",
                            target == "journalist" ~ "Journalists",
                            target == "former government official" ~ "Former government \n officials",
                            target == "opposition" ~ "Opposition members",
                            T ~ NA_character_))

ggplot(data = AAAD_targets, aes(x = n, y= target)) +
  geom_bar(stat="identity", width = 0.75,  aes(reorder(target, n), n), alpha = .75) +
  theme_bw() +
  xlab("") +
  ylab("Transnational repression events") +
  coord_flip()

ggsave("figures/Figure_A1.pdf", width = 7, height = 5,
       dpi = "retina")
# ------------------------------------------- #

###### Figure 4: Histogram ######
# load data
load("data/data_final.Rda")

ggplot(data_final, aes(x = all_events)) +
  geom_histogram(bins = 20) + xlim(-1,20) +
  xlab("Transnational repression events") + ylab("Count") +
  theme_bw()

ggsave("figures/Figure_A2.pdf", width = 7, height = 5,
       dpi = "retina")
